package gui;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JPasswordField;

@SuppressWarnings("serial")
public class Register extends JFrame {

	private JPanel contentPane;
	private JTextField firstNameTextField;
	private JTextField lastNameTextField;
	private JTextField addressTextField;
	private JTextField userNameRegTextField;
	private JTextField emailTextField;
	private JButton btnReRegister;
	private JPasswordField passwordRefField;
	private JPasswordField confPasswordField;
	private JTextField mobileNumTextField;



	/**
	 * Create the frame.
	 */
	public Register() {
		setTitle("Registeration");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(250, 30, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblUserName = new JLabel("User name");
		lblUserName.setBounds(53, 110, 74, 16);
		contentPane.add(lblUserName);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(87, 138, 38, 16);
		contentPane.add(lblEmail);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(64, 166, 63, 16);
		contentPane.add(lblPassword);
		
		JLabel lblConfirmPassword = new JLabel("Confirm password");
		lblConfirmPassword.setBounds(6, 194, 119, 16);
		contentPane.add(lblConfirmPassword);
		
		JLabel lblFirstName = new JLabel("First name");
		lblFirstName.setBounds(53, 26, 74, 16);
		contentPane.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last name");
		lblLastName.setBounds(53, 54, 72, 16);
		contentPane.add(lblLastName);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(66, 82, 61, 16);
		contentPane.add(lblAddress);
		
		firstNameTextField = new JTextField();
		firstNameTextField.setBounds(128, 20, 134, 28);
		contentPane.add(firstNameTextField);
		firstNameTextField.setColumns(10);
		
		lastNameTextField = new JTextField();
		lastNameTextField.setBounds(128, 48, 134, 28);
		contentPane.add(lastNameTextField);
		lastNameTextField.setColumns(10);
		
		addressTextField = new JTextField();
		addressTextField.setBounds(128, 76, 134, 28);
		contentPane.add(addressTextField);
		addressTextField.setColumns(10);
		
		userNameRegTextField = new JTextField();
		userNameRegTextField.setBounds(128, 104, 134, 28);
		contentPane.add(userNameRegTextField);
		userNameRegTextField.setColumns(10);
		
		emailTextField = new JTextField();
		emailTextField.setBounds(128, 132, 134, 28);
		contentPane.add(emailTextField);
		emailTextField.setColumns(10);
		
		btnReRegister = new JButton("Register");
		btnReRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String firstName = firstNameTextField.getText();
				String lastName = lastNameTextField.getText();
				String userName = userNameRegTextField.getText();
				String email = emailTextField.getText();
				@SuppressWarnings("deprecation")
				String password = passwordRefField.getText();
				@SuppressWarnings("deprecation")
				String confPass = confPasswordField.getText();
				String address = addressTextField.getText();
				String mobileNum = mobileNumTextField.getText();
			    Pattern pattern = Pattern.compile("\\d{3}\\d{7}");
			    Matcher matcher = pattern.matcher(mobileNum);
				
				if (firstName.isEmpty() || lastName.isEmpty() || userName.isEmpty() || email.isEmpty() || password.isEmpty() || confPass.isEmpty() || address.isEmpty()) 
					JOptionPane.showMessageDialog(null, "Some of the requierd fields are EMPTY please fill it !! ");
				else {
					String arr[] = email.split("@");
					if (arr.length != 2) 
						JOptionPane.showMessageDialog(null, "Incorrect Email please enter it again");
					else{
						String str[]= arr[1].split("\\.");
						if(str.length == 1)
							JOptionPane.showMessageDialog(null, "Incorrect Email please enter it again");
						else{
							if (password.length()<5)
								JOptionPane.showMessageDialog(null, "Password Must be at least 5 charchters");
							else{
								if (!password.equals(confPass))
									JOptionPane.showMessageDialog(null, "The Passwords does not match");
								else{
									if(!matcher.matches())
										JOptionPane.showMessageDialog(null, "Phone Number must be in the form XXXXXXXXXX");
								}
							}
						}	
					}
				}
			}
		});
		btnReRegister.setBounds(316, 243, 117, 29);
		contentPane.add(btnReRegister);
		
		passwordRefField = new JPasswordField();
		passwordRefField.setBounds(128, 160, 134, 28);
		contentPane.add(passwordRefField);
		
		confPasswordField = new JPasswordField();
		confPasswordField.setBounds(128, 188, 134, 28);
		contentPane.add(confPasswordField);
		
		JLabel lblMobileNumber = new JLabel("Mobile number");
		lblMobileNumber.setBounds(28, 222, 94, 16);
		contentPane.add(lblMobileNumber);
		
		mobileNumTextField = new JTextField();
		mobileNumTextField.setBounds(128, 216, 134, 28);
		contentPane.add(mobileNumTextField);
		mobileNumTextField.setColumns(10);
	}
}
